package hsp.admin.app.literature.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.literature.po.TagPO;
import hsp.admin.app.literature.service.ITagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "TagController", description = "标签管理")
@RestController
@RequestMapping("literature/tag")
public class TagController {

    private final ITagService service;

    @Autowired
    public TagController(
        ITagService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取标签")
    @GetMapping("{id}")
    public ApiResult<TagPO> getTag(@PathVariable("id") long id){
        TagPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取标签分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<TagPO>> getTagPage(@PathVariable int page, @PathVariable int size, TagPO po){
        LambdaQueryWrapper<TagPO> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(po.getName())) wrapper.like(TagPO::getName, po.getName());
        if(StringUtils.isNotBlank(po.getType())) wrapper.eq(TagPO::getType, po.getType());
        Page<TagPO> records = service.page(new Page<>(page, size), wrapper);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑标签")
    @PostMapping()
    public ApiResult<?> postOrPutTag(@RequestBody TagPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除标签")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteTag(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
